package com.hp.easyBuy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hp.easyBuy.contants.RedixKey;
import com.hp.easyBuy.dao.GoodsCategoryDao;
import com.hp.easyBuy.dao.GoodsDao;
import com.hp.easyBuy.entity.DTO.TGoodsCategoryDTO;
import com.hp.easyBuy.entity.TGoods;
import com.hp.easyBuy.entity.TGoodsCategory;
import com.hp.easyBuy.service.GoodsService;
import com.hp.easyBuy.utils.MyPage;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, TGoods> implements GoodsService {
    @Resource
    private GoodsDao goodsDao;

    @Resource
    private GoodsCategoryDao goodsCategoryDao;

    @Resource
    private RedisTemplate redisTemplate;


    @Override
    public Page doListDb(Integer currentpage,Integer pagesize,Integer categoryid) {
        Page page = new Page(currentpage, pagesize);
        QueryWrapper queryWrapper = new QueryWrapper();
        //限定商品类别和已经上架
        queryWrapper.eq("categoryid", categoryid);
        queryWrapper.eq("goodsstatus", 1);
        return goodsDao.selectMapsPage(page, queryWrapper);
    }

    @Override
    public MyPage<TGoods> doListRediS(Integer currentpage, Integer pagesize, Integer categoryid) {
       //从redis里面获取上父类id
        Integer parentid  = (Integer)redisTemplate.opsForValue().get(RedixKey.GOODS_CATEGORY_PARENTID + categoryid);
        //如果parentid 为null那么从数据库获取该数据，再传给redix
        if(parentid == null){
            QueryWrapper<TGoodsCategory> queryWrapper = new QueryWrapper<TGoodsCategory>();
            List<TGoodsCategory> list = goodsCategoryDao.selectList(queryWrapper);
            //然后将数据依次循环传给redis
            for(TGoodsCategory tGoodsCategory : list){
                redisTemplate.opsForValue().set(RedixKey.GOODS_CATEGORY_PARENTID + tGoodsCategory.getCategoryid(), tGoodsCategory.getParentid());
            }
            //最后再从redis里面取出该数据赋值给parentid
            parentid = (Integer)redisTemplate.opsForValue().get(RedixKey.GOODS_CATEGORY_PARENTID + categoryid);
        }
        //从redis取值的pattern
        String pattern = null;
        if(parentid == 1){
            //如果父类id是1，证明是大类
            pattern = RedixKey.GOODS_LIST + categoryid ;
        }else{
            //如果不是1，证明是小类
            pattern = RedixKey.GOODS_LIST + parentid + ":" + categoryid;
        }
        System.out.println("pattern = " + pattern);
        //根据pattern从redis里面取出对应数据
        Set<String> keys = redisTemplate.keys(pattern +"*");
        System.out.println("keys = "+keys);
        System.out.println("size = " + keys.size());
        if(keys.size() == 0){
            //如果redis里面没有数据，那么从数据库里面获取数据然后传递到redis当中
            QueryWrapper<TGoods> queryWrapper = new QueryWrapper<TGoods>();
            List<TGoods> goodsList = goodsDao.selectList(queryWrapper);
            System.out.println("goodlist = "+goodsList);
            for(TGoods goods : goodsList){
                //更具goods对应的类别找到三级标签，再根据三级bi
                redisTemplate.opsForValue().set( pattern + ":" +goods.getGoodsid(),goods);
            }
            keys = redisTemplate.keys(pattern +"*");

        }
        List<TGoods> goodsList = new ArrayList<TGoods>();
        int i = 0;
        for(String key : keys){
            if(i >= (currentpage -1) * pagesize && i < currentpage * pagesize){
                goodsList.add((TGoods) redisTemplate.opsForValue().get(key));
            }
            i++;
        }
        MyPage<TGoods> page = new MyPage<TGoods>();
        page.setCurrentPage(currentpage);
        page.setPageSize(pagesize);
        page.setTotalCount((long) keys.size());
        page.setList(goodsList);
        return page;
    }

    @Override
    public Page<TGoods> indexGoodList(Integer promotiontype) {
        Page page = new Page<>(1,7);
        QueryWrapper<TGoods> queryWrapper = new QueryWrapper<TGoods>();
        //是今日特价还是热卖推荐
        queryWrapper.eq("PROMOTIONTYPE", promotiontype);
        //保证是上架的内容才会在上面
        queryWrapper.eq("GOODSSTATUS", 1);
        queryWrapper.orderByDesc("GOODSID");
        Page pageList = goodsDao.selectMapsPage(page, queryWrapper);
        return pageList;
    }

    @Override
    public List<TGoodsCategoryDTO> goodsLevelList() {
        //获取所有的二类商品并且封装成集合
        QueryWrapper<TGoodsCategory> queryWrapper = new QueryWrapper<TGoodsCategory>();
        queryWrapper.eq("LEVEL", 2);
        queryWrapper.orderByAsc("LEVEL");
        List<TGoodsCategory> tGoodsCategories = goodsCategoryDao.selectList(queryWrapper);
        List<TGoodsCategoryDTO> tGoodsCategoryDTOList = new ArrayList<TGoodsCategoryDTO>();
        for(TGoodsCategory tGoodsCategory : tGoodsCategories){
            //检索2级商品下面的三级商品
            QueryWrapper<TGoodsCategory> queryWrapper2 = new QueryWrapper<TGoodsCategory>();
            queryWrapper2.eq("PARENTID", tGoodsCategory.getCategoryid());
            queryWrapper2.orderByAsc("CATEGORYID");
            List<TGoodsCategory> tGoodsCategoriesList = goodsCategoryDao.selectList(queryWrapper2);
            TGoodsCategoryDTO tGoodsCategoryDTO = new TGoodsCategoryDTO(tGoodsCategory);
            tGoodsCategoryDTO.setGoodsCategoriesList(tGoodsCategoriesList);
            tGoodsCategoryDTOList.add(tGoodsCategoryDTO);
        }
        return tGoodsCategoryDTOList;
    }

    @Override
    public Page<TGoods> doPage(Integer currentpage, Integer pagesize, Map<String, Object> params) {
        Page<TGoods> page = new Page<TGoods>(currentpage, pagesize);
        QueryWrapper<TGoods> queryWrapper = new QueryWrapper<TGoods>();
        if(params.get("parentid") != null && !params.get("parentid").equals("")){
            queryWrapper.eq("gc.PARENTID", params.get("parentid"));
        }
        if(params.get("subid") != null && !params.get("subid").equals("")){
            queryWrapper.eq("g.CATEGORYID", params.get("subid"));
        }
        return goodsDao.doPage(page,queryWrapper);
    }


}
